package com.robusta.app.matchers;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

public class CompositeMatcher<T> extends TypeSafeMatcher<T> {
    List<TypeSafeMatcher<T>> matchers = newArrayList();

    public void add(TypeSafeMatcher<T> aMatcher) {
        matchers.add(aMatcher);
    }
    @Override
    public boolean matchesSafely(T toBeMatched) {
        for (TypeSafeMatcher<T> matcher : matchers) {
            if(!matcher.matchesSafely(toBeMatched)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        for (TypeSafeMatcher<T> matcher : matchers) {
            matcher.describeTo(description);
            description.appendText(" and ");
        }
    }
}
